<?php

class SiteController extends Controller
{
	

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{   
            //echo 'index';

            //print_r('<pre>');
            //print_r($this->getRules());
            //print_r('</pre>');
            //exit();
            
            if(Yii::app()->user->isGuest){
                $this->redirect(array('site/login'));
            }
            
            $pagevals = array(
                'page_title' => "Trang quản trị",
                'subpage_title' => Yii::app()->params['pageName'],
                //'action' => 'game/abc'
            );
            
            
            //$pagevals['order_chart'] = $this->get_views();
            
            $pagevals['donut_chart'] = isset($_GET['donut_chart']) ? $_GET['donut_chart'] : 'Post';
            $pagevals['status_donut_chart'] = isset($_GET['status_donut_chart']) ? $_GET['status_donut_chart'] : 'all';
            $pagevals['top_view_type'] = isset($_GET['top_view_type']) ? $_GET['top_view_type'] : 'post';
            
            
            
            if($pagevals['donut_chart'] == 'all'){
                $pagevals['donut_chart_data'] = $this->get_donut_chart($pagevals['donut_chart']);
            }else{
                $pagevals['donut_chart_data'] = $this->get_donut_chart($pagevals['donut_chart'],$pagevals['status_donut_chart']);
            }
            
                
           $this->renderPartial('index', array('page_title'=>'Trang quản trị', 'pagevals'=>$pagevals));
	
            //$this->display('site/index', array('page_title'=>'Trang quản trị', 'pagevals'=>$pagevals));

	}
        /*
        private function getTopView($type){
            $data = TrackingModel::model()->getByConditions(array('type'=>$type), 5, array('views'=>'desc'), array('views','_id'));
            
            $model = ucfirst($type).'Model';
            foreach($data as $key => $val){
                $value = PostModel::model()->findByPk($val['_id']);
                if($value != null){
                    if($type == '$datagame'){
                        $data[$key]['title'] = substr($value['name'], 0, 50);
                    }else{
                        $data[$key]['title'] = substr($value['title'], 0, 50);
                    }
                }else{
                    $data[$key]['title'] = '';
                }
            }
            
            //print_r('<pre>');
            //print_r($data);
            //print_r('</pre>');
            //exit();
            return ;
        }
       
        private function get_views(){
            $game_views = TrackingModel::model()->getByConditions(array('type'=>'game'), 0, '', array('views')); 
            $post_views = TrackingModel::model()->getByConditions(array('type'=>'post'), 0, '', array('views')); 
            $entertainment_views = TrackingModel::model()->getByConditions(array('type'=>'entertainment'), 0, '', array('views')); 
            $result = array('game'=>0,'post'=>0,'entertainment'=>0);
            
            if(is_array($game_views)){
                foreach($game_views as $val){
                    $result['game'] = $result['game'] + $val['views'];
                }
            }
            
            if(is_array($post_views)){
                foreach($post_views as $val){
                    $result['post'] = $result['post'] + $val['views'];
                }
            }
            
            if(is_array($entertainment_views)){
                foreach($entertainment_views as $val){
                    $result['entertainment'] = $result['entertainment'] + $val['views'];
                }
            }
            
            return $result;
        }
        */
        
        private function get_donut_chart($name,$status = 'all'){
            $model = $name.'Model';
            $model_category = $name[0].'CategoryModel';
            
            $category_filter_laber = array();
            $categories = PCategoryModel::model()->getAll();
            if(is_array($categories)){
                foreach($categories as $value){
                    if($value != null){ 
                       $category_filter_laber[$value['id']]  = $value['name'];
                    }
                }
            }
            $result = array();
            if($status == 'all'){
                foreach($category_filter_laber as $key=>$value){
                    $result[$value] = PostModel::model()->countRow(array('category_id'=>$key));
                }
                return $result;
            }else{
                foreach($category_filter_laber as $key=>$value){
                    $result[$value] = PostModel::model()->countRow(array('category_id'=>$key,'status'=>$status));
                }
                return $result;
            }
            
        }
  
  	/**
	 * This is the action to handle external exceptions.
	 */
	public function actionError()
	{
 
		if($error=Yii::app()->errorHandler->error)
		{   
                    //if($error['type'] == 403)
                    $pagevals = array(
                        'page_title' => "Trang quản trị",
                        'subpage_title' => Yii::app()->params['pageName'],
                        //'action' => 'game/abc'
                    );
                    $this->renderPartial('error',array('pagevals'=>$pagevals,'error'=>$error));
		}
             
             
	}

	public function actionLogin()
	{
            //echo 'login';
            
            $pagevals = array(
                'page_title' => "Trang quản trị",
                'subpage_title' => Yii::app()->params['pageName'],
                'error'=>'',
                'username'=>'',
                //'action' => 'game/abc'
            );
            //print_r($pagevals);
            
            if(isset($_POST['username']) && isset($_POST['password'])){
                $user = new UserIdentity($_POST['username'],  md5($_POST['password']));

                if($user->authenticate()){
                     Yii::app()->user->login($user,3600*24*7);
                     //$this->redirect(Yii::app()->request->urlReferrer);
                     $this->redirect(array('site/index'));
                }
                else{
                    $pagevals['error'] = $user->errorCode;
                    $pagevals['username']= $_POST['username'];
                }
            }
            
            if(!Yii::app()->user->isGuest){
                $this->redirect(array('site/index'));
            }
            
            $this->renderPartial('login', array('pagevals'=>$pagevals));
             
            
	}
        
        
        

	/**
	 * Logs out the current user and redirect to homepage.
	 */
	public function actionLogout()
	{
          
		Yii::app()->user->logout();
		$this->redirect(Yii::app()->homeUrl);
             
	}
}